package com.example.design.factory;

import cn.hutool.core.util.EnumUtil;
import com.example.design.bo.PaymentBody;
import com.example.design.constant.PayStrategyEnum;
import com.example.design.service.IPayment;
import com.example.design.service.PaymentContext;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author Hong Qiang
 * @Date 2023/3/27 14:33
 * @Version 1.0
 */
@Component
public class PaymentStrategyHandler {

    public static Boolean pay(PaymentBody payBody) {
        if (!EnumUtil.contains(PayStrategyEnum.class, payBody.getType())) {
            throw new IllegalArgumentException("不支持的支付方式!");
        }
        // 1.获取支付策略对象
        IPayment payStrategy = PaymentFactory.getPayStrategy(payBody.getType());
        // spring.factotries.获取支付策略上下文
        PaymentContext paymentContext = new PaymentContext(payStrategy);
        // 3.进行支付
        return paymentContext.pay(payBody);
    }
}
